package com.qxsx.midi.adapter;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.me.DownLodeMp3Activity;
import com.qxsx.midi.activity.me.MeKejiantActivity;
import com.qxsx.midi.activity.personal.DownLodeMusciActivity;
import com.qxsx.midi.config.AppContext;
import com.qxsx.midi.db.MyMusicFileDBTool;
import com.qxsx.midi.model.MyFile;
import com.qxsx.midi.model.MyMusicFile;
import com.qxsx.midi.model.Order;
import com.qxsx.midi.utils.CustomProgress;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.SPUtils;
import com.qxsx.midi.utils.Tools;
import com.qxsx.midi.utils.callBackJsonArray;
import com.qxsx.midi.utils.callBackNull;
import com.qxsx.midi.view.ZListView.BaseSwipeAdapter;
import com.qxsx.midi.view.ZListView.DragEdge;
import com.qxsx.midi.view.ZListView.ShowMode;
import com.qxsx.midi.view.ZListView.SimpleSwipeListener;
import com.qxsx.midi.view.ZListView.ZSwipeItem;

import org.json.JSONArray;
import org.json.JSONObject;
import org.w3c.dom.Text;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/1/19.
 */
public class DownlodeMusicAdapter extends BaseSwipeAdapter {

    private Context context;
    private List<MyMusicFile> data = new ArrayList<>();
    private LayoutInflater listContainer; // 视图容器
    private Activity activity;


    private int myPosition;

    public DownlodeMusicAdapter(Context context, List<MyMusicFile> arrayList, Activity activity) {
        super();
        this.context = context;
        listContainer = LayoutInflater.from(context); // 创建视图容器并设置上下文
        this.data = arrayList;
        this.activity = activity;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getSwipeLayoutResourceId(int position) {
        return R.id.swipe_item;
    }

    @Override
    public View generateView(int position, ViewGroup parent) {
        return listContainer.inflate(R.layout.downlode_item, parent, false);
    }

    @Override
    public void fillValues(final int position, View convertView) {

        final ZSwipeItem swipeItem = (ZSwipeItem) convertView.findViewById(R.id.swipe_item);
        final MyMusicFile myMusicFile = data.get(position);
        ViewHolder viewHolder = new ViewHolder();

        viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
        viewHolder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
        viewHolder.tv_name.setText(myMusicFile.get_name());
        viewHolder.tv_content.setText(myMusicFile.get_content());



        LinearLayout ll = (LinearLayout) convertView.findViewById(R.id.ll);


        convertView.setTag(viewHolder);


        swipeItem.setShowMode(ShowMode.PullOut);
        swipeItem.setDragEdge(DragEdge.Right);


        swipeItem.addSwipeListener(new SimpleSwipeListener() {

            @Override
            public void onOpen(ZSwipeItem layout) {
                myPosition = position;
            }

            @Override
            public void onClose(ZSwipeItem layout) {
                myPosition = position;
            }

            @Override
            public void onStartOpen(ZSwipeItem layout) {
                myPosition = position;
            }

            @Override
            public void onStartClose(ZSwipeItem layout) {
                myPosition = position;
            }

            @Override
            public void onHandRelease(ZSwipeItem layout, float xvel, float yvel) {
                myPosition = position;
            }

            @Override
            public void onUpdate(ZSwipeItem layout, int leftOffset, int topOffset) {
            }
        });

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, DownLodeMp3Activity.class);
                intent.putExtra("_name", myMusicFile.get_name());
                intent.putExtra("_content", myMusicFile.get_content());
                intent.putExtra("_music",myMusicFile.get_music());

                activity.startActivity(intent);
            }
        });


        ll.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                MyMusicFileDBTool myFileDBTool = null; // 执行对数据库的相关操作
                myFileDBTool = new MyMusicFileDBTool(activity);
                myFileDBTool.deleteOne(myMusicFile.get_id()); // 数据源，从数据库中读取而来
                File deletefile = new File(myMusicFile.get_music());
                deletefile.delete();
                data.remove(position);
                swipeItem.close(false);
                notifyDataSetChanged();
            }
        });
    }


    public void AddData(List<MyMusicFile> list, boolean flag) {
        for (MyMusicFile myMusicFile : list) {
            data.add(myMusicFile);
        }
        notifyDataSetChanged();
    }

    public void RemoveAll() {
        if (data != null) {
            this.data.clear();
            notifyDataSetChanged();
        }
    }




    class ViewHolder {
        TextView tv_name, tv_content;

    }
}
